package com.ark.cloud.dataservices.modules.sys.controller;

import com.ark.cloud.dataservices.common.web.entity.Result;
import com.ark.cloud.dataservices.modules.sys.entity.form.ModuleForm;
import com.ark.cloud.dataservices.modules.sys.entity.form.SeatForm;
import com.ark.cloud.dataservices.modules.sys.entity.po.Module;
import com.ark.cloud.dataservices.modules.sys.entity.po.Seat;
import com.ark.cloud.dataservices.modules.sys.service.IModuleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/module")
@Slf4j
public class ModuleController {

    private final IModuleService moduleService;

    public ModuleController(IModuleService moduleService) {
        this.moduleService = moduleService;
    }

    /**
     * 新增模块
     *
     * @param moduleForm {@link SeatForm}
     * @return {@link Result}
     */
    @PostMapping
    public Result<?> add(@RequestBody ModuleForm moduleForm) {
        log.debug("name:{}", moduleForm);
        if (StringUtils.isBlank(moduleForm.getName())) {
            return Result.fail(50000, "模块名不能为空");
        }
        if (StringUtils.isBlank(moduleForm.getUrl())) {
            return Result.fail(50000, "模块 url 不能为空");
        }
        Module module = moduleForm.toPo(Module.class);
        return Result.success(moduleService.add(module));
    }

    /**
     * 删除模块
     *
     * @param id 模块 id
     * @return {@link Result}
     */
    @DeleteMapping(value = "/{id}")
    public Result<?> delete(@PathVariable String id) {
        return Result.success(moduleService.delete(id));
    }

    /**
     * 修改模块
     *
     * @param id         模块 id
     * @param moduleForm {@link SeatForm}
     * @return {@link Result}
     */
    @PutMapping(value = "/{id}")
    public Result<?> update(@PathVariable String id, @Valid @RequestBody ModuleForm moduleForm) {
        log.debug("update with id:{}", id);
        Module module = moduleForm.toPo(Module.class);
        module.setId(id);
        return Result.success(moduleService.update(module));
    }

    /**
     * 获取模块
     *
     * @param id 模块 id
     * @return {@link Result}
     */
    @GetMapping(value = "/{id}")
    public Result<?> get(@PathVariable String id) {
        log.debug("get with id:{}", id);
        return Result.success(moduleService.get(id));
    }

    /**
     * 模块列表
     *
     * @return {@link Result}
     */
    @PostMapping("/list")
    public Result<?> list(@RequestBody ModuleForm moduleForm) {
        log.debug("query modules with param:{}", moduleForm);
        QueryWrapper<Module> moduleQueryWrapper = new QueryWrapper<>();
        moduleQueryWrapper.eq(StringUtils.isNotBlank(moduleForm.getType()), "type", moduleForm.getType());
        List<Module> moduleList = moduleService.list(moduleQueryWrapper);
        moduleList.forEach(m -> m.setSeatNames(moduleService.getSeatNamesByModuleId(m.getId())));
        return Result.success(moduleList);
    }

    @PutMapping(value = "/seats/{id}")
    public Result<?> addSeatsToUser(@PathVariable String id, @RequestBody Set<String> seatIds) {
        log.debug("save Batch {}", id);
        return Result.success(moduleService.addSeatsToModuleBySeatIds(id, seatIds));
    }
}